/*
 * The Open Source Unix Installer
 *
 * The MIT License
 *
 * Copyright 2011-2012 Andrey Pudov.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * @author  Andrey Pudov    <andrey@andreypudov.com>
 * @version 0.00.00
 * @name    u_xml_example.h
 * @date:   Jul 28, 2012
 */

#ifndef INSTALLER_UTIL_XML_U_EXAMPLE_H_
#define INSTALLER_UTIL_XML_U_EXAMPLE_H_

#include <locale.h>     /* setlocale() */
#include <stdio.h>
#include <stdbool.h>

#include "xml_reader.h"

#include "u_main.h"

#ifdef __cplusplus
extern "C" {
#endif

bool u_installer_xml_example_start();              /* unit entry point */

bool u_installer_xml_example_example1();
bool u_installer_xml_example_example2();

bool u_installer_xml_example_start() {
    return (u_installer_xml_example_example1()
    	&& u_installer_xml_example_example2());
}

bool u_installer_xml_example_example1() {
    XMLReader    *reader    = NULL;
    XMLDocument  *document  = NULL;
    XMLElement   *element   = NULL;
    XMLElement   *element_t = NULL;
    XMLAttribute *attribute = NULL;
    
    wchar_t *element_name     = NULL;
    wchar_t *element_value    = NULL;
    wchar_t *attribute_name   = NULL;
    wchar_t *attribute_value  = NULL;
    
    /* xml file content */
    const wchar_t *xml_example1 = L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n \
                                    <config>\n \
                                        <entry key=\"Key1\" Value=\"Value1\">Entry1</entry>\n \
                                        <entry key=\"Key2\" Value=\"Value2\">Entry2</entry>\n \
                                        <entry key=\"Key3\" Value=\"Value3\">Entry3</entry>\n \
                                    </config>";
                               
    setlocale(LC_ALL, "en_US.UTF-8");
    
    /* creating the file */                       
    char file_name[] = "/tmp/installer-xml_reader_example1";
    FILE *pointer    = fopen(file_name, "w+");
     
    if (pointer == NULL) {
        fprintf(stderr, "[ERROR|%s] %s - %s\n",
                "u_installer_xml_example_example1", file_name, strerror(errno));
        return false;
    }
    
    fwprintf(pointer, L"%ls\n", xml_example1);
    fclose(pointer);
    
    pointer = fopen(file_name, "r");
    if (pointer == NULL) {
        fprintf(stderr, "[ERROR|%s] %s - %s\n",
                "u_installer_xml_example_example1", file_name, strerror(errno));
        return false;
    }
    
    /* read the file and verifying the structure */
    reader   = installer_xml_reader_new(pointer);
    if (reader == NULL) {
        u_installer_main_print("xml_example", "example1(1)", false);
        return false;
    }
    
    document = installer_xml_reader_read(reader);
    if ((document == NULL) || (document->elements == NULL)) {
        u_installer_main_print("xml_example", "example1(2)", false);
        return false;
    }
    
    /* 
     * main example code 
     */
     
    /* XML DOCUMENT v1.0 */
    element = installer_xml_document_get_element(document, NULL);
    element_name  = installer_xml_element_get_name(element);
    element_value = installer_xml_element_get_value(element);
    printf("%ls %ls\n", element_name, element_value);
    
    /* <?xml version="1.0" encoding="UTF-8"?> */
    element_t = installer_xml_element_get_element(element, NULL);
    attribute = installer_xml_element_get_attribute(element_t, NULL);
    element_name    = installer_xml_element_get_name(element_t);
    attribute_name  = installer_xml_attribute_get_name(attribute);
    attribute_value = installer_xml_attribute_get_value(attribute);
    printf("<?%ls %ls=\"%ls\" ", 
                    element_name, attribute_name, attribute_value);
    attribute = installer_xml_element_get_attribute(element_t, attribute);
    attribute_name  = installer_xml_attribute_get_name(attribute);
    attribute_value = installer_xml_attribute_get_value(attribute);
    printf("%ls=\"%ls\"?>\n", 
                    attribute_name, attribute_value);
    
    /* <config> */
    element = installer_xml_element_get_element(element, element_t);
    printf("<%ls>\n", installer_xml_element_get_name(element));
    
    /* nullify temporary pointer to use it as iterator */
    element_t = NULL;
    while ((element_t = installer_xml_element_get_element(element, element_t)) != NULL) {
        attribute = installer_xml_element_get_attribute(element_t, NULL);
        element_name    = installer_xml_element_get_name(element_t);
        element_value   = installer_xml_element_get_value(element_t);
        attribute_name  = installer_xml_attribute_get_name(attribute);
        attribute_value = installer_xml_attribute_get_value(attribute);
        printf("\t<%ls %ls=\"%ls\" ", 
                    element_name, attribute_name, attribute_value);
        attribute = installer_xml_element_get_attribute(element_t, attribute);
        attribute_name  = installer_xml_attribute_get_name(attribute);
        attribute_value = installer_xml_attribute_get_value(attribute);
        printf("%ls=\"%ls\">%ls</%ls>\n", 
                    attribute_name, attribute_value, element_value, element_name);
    }
    
    /* free memory and remove temporary file */
    installer_xml_reader_delete(reader);
    fclose(pointer);
    remove(file_name);

    u_installer_main_print("xml_example", "example1()", true);
    
    return true;
}

bool u_installer_xml_example_example2() {
    XMLReader    *reader    = NULL;
    XMLDocument  *document  = NULL;
    XMLElement   *element   = NULL;
    XMLElement   *element_t = NULL;
    XMLElement   *element_c = NULL; /* the pointer to book's category */
    XMLElement   *element_b = NULL; /* the pointer to book entry */
    XMLAttribute *attribute = NULL;
    
    wchar_t *element_value    = NULL;
    wchar_t *attribute_value  = NULL;
    
    /* xml file content */
    const wchar_t *xml_example2 = L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n \
                                    <books>\n \
                                        <category name=\"Art & Photography\">\n \
                                            <book author=\"Alex Johnson\" published=\"01 May 2012\">Bookshelf</book>\n \
                                            <book author=\"Andrew Blauner\" published=\"24 Apr 2012\">Central Park</book>\n \
                                        </category>\n \
                                        <category name=\"Romance\">\n \
                                            <book author=\"Gena Showalter\" published=\"05 Jul 2012\">Wicked Nights</book>\n \
                                            <book author=\"Elle Kennedy\" published=\"01 May 2012\">Midnight Rescue</book>\n \
                                        </category>\n \
                                    </books>";
                               
    setlocale(LC_ALL, "en_US.UTF-8");
    
    /* creating the file */                       
    char file_name[] = "/tmp/installer-xml_reader_example2";
    FILE *pointer    = fopen(file_name, "w+");
     
    if (pointer == NULL) {
        fprintf(stderr, "[ERROR|%s] %s - %s\n",
                "u_installer_xml_example_example2", file_name, strerror(errno));
        return false;
    }
    
    fwprintf(pointer, L"%ls\n", xml_example2);
    fclose(pointer);
    
    pointer = fopen(file_name, "r");
    if (pointer == NULL) {
        fprintf(stderr, "[ERROR|%s] %s - %s\n",
                "u_installer_xml_example_example2", file_name, strerror(errno));
        return false;
    }
    
    /* read the file and verifying the structure */
    reader   = installer_xml_reader_new(pointer);
    if (reader == NULL) {
        u_installer_main_print("xml_example", "example2(1)", false);
        return false;
    }
    
    document = installer_xml_reader_read(reader);
    if ((document == NULL) || (document->elements == NULL)) {
        u_installer_main_print("xml_example", "example2(2)", false);
        return false;
    }
    
    /* 
     * main example code 
     */
     
    /* XML DOCUMENT v1.0 */
    element = installer_xml_document_get_element(document, NULL);
    
    /* <?xml version="1.0" encoding="UTF-8"?> */
    element_t = installer_xml_element_get_element(element, NULL);
    
    /* <books> */
    element = installer_xml_element_get_element(element, element_t);
    
    /* nullify temporary pointer to use it as iterator */
    while ((element_c = installer_xml_element_get_element(element, element_c)) != NULL) {
        attribute       = installer_xml_element_get_attribute(element_c, NULL);
        attribute_value = installer_xml_attribute_get_value(attribute);
        printf("%ls\n", attribute_value);
        
        while ((element_b = installer_xml_element_get_element(element_c, element_b)) != NULL) {
            element_value   = installer_xml_element_get_value(element_b);
            attribute       = installer_xml_element_get_attribute(element_b, NULL);
            attribute_value = installer_xml_attribute_get_value(attribute);
            printf("\t%ls by %ls ", element_value, attribute_value);
            
            attribute       = installer_xml_element_get_attribute(element_b, attribute);
            attribute_value = installer_xml_attribute_get_value(attribute);
            printf("(%ls)\n", attribute_value);
        }
    }
    
    /* free memory and remove temporary file */
    installer_xml_reader_delete(reader);
    fclose(pointer);
    remove(file_name);

    u_installer_main_print("xml_example", "example2()", true);
    
    return true;
}

#ifdef __cplusplus
}
#endif

#endif /* INSTALLER_UTIL_XML_U_EXAMPLE_H_ */ 
